// Copyright (c) Lawrence Livermore National Security, LLC and other VisIt
// Project developers.  See the top-level LICENSE file for dates and other
// details.  No copyright assignment is required to contribute to VisIt.

#include <PyQueryAttributes.h>
#include <ObserverToCallback.h>
#include <stdio.h>
#include <Py2and3Support.h>

// ****************************************************************************
// Module: PyQueryAttributes
//
// Purpose:
//   This class contains attributes used for query.
//
// Note:       Autogenerated by xml2python. Do not modify by hand!
//
// Programmer: xml2python
// Creation:   omitted
//
// ****************************************************************************

//
// This struct contains the Python type information and a QueryAttributes.
//
struct QueryAttributesObject
{
    PyObject_HEAD
    QueryAttributes *data;
    bool        owns;
    PyObject   *parent;
};

//
// Internal prototypes
//
static PyObject *NewQueryAttributes(int);
std::string
PyQueryAttributes_ToString(const QueryAttributes *atts, const char *prefix, const bool forLogging)
{
    std::string str;
    char tmpStr[1000];

    snprintf(tmpStr, 1000, "%sresultsMessage = \"%s\"\n", prefix, atts->GetResultsMessage().c_str());
    str += tmpStr;
    {   const doubleVector &resultsValue = atts->GetResultsValue();
        snprintf(tmpStr, 1000, "%sresultsValue = (", prefix);
        str += tmpStr;
        for(size_t i = 0; i < resultsValue.size(); ++i)
        {
            snprintf(tmpStr, 1000, "%g", resultsValue[i]);
            str += tmpStr;
            if(i < resultsValue.size() - 1)
            {
                snprintf(tmpStr, 1000, ", ");
                str += tmpStr;
            }
        }
        snprintf(tmpStr, 1000, ")\n");
        str += tmpStr;
    }
    {   const floatVector &floatResultsValue = atts->GetFloatResultsValue();
        snprintf(tmpStr, 1000, "%sfloatResultsValue = (", prefix);
        str += tmpStr;
        for(size_t i = 0; i < floatResultsValue.size(); ++i)
        {
            snprintf(tmpStr, 1000, "%f", floatResultsValue[i]);
            str += tmpStr;
            if(i < floatResultsValue.size() - 1)
            {
                snprintf(tmpStr, 1000, ", ");
                str += tmpStr;
            }
        }
        snprintf(tmpStr, 1000, ")\n");
        str += tmpStr;
    }
    snprintf(tmpStr, 1000, "%stimeStep = %d\n", prefix, atts->GetTimeStep());
    str += tmpStr;
    snprintf(tmpStr, 1000, "%sxUnits = \"%s\"\n", prefix, atts->GetXUnits().c_str());
    str += tmpStr;
    snprintf(tmpStr, 1000, "%syUnits = \"%s\"\n", prefix, atts->GetYUnits().c_str());
    str += tmpStr;
    snprintf(tmpStr, 1000, "%sfloatFormat = \"%s\"\n", prefix, atts->GetFloatFormat().c_str());
    str += tmpStr;
    snprintf(tmpStr, 1000, "%sxmlResult = \"%s\"\n", prefix, atts->GetXmlResult().c_str());
    str += tmpStr;
    //queryInputParams
    return str;
}

static PyObject *
QueryAttributes_Notify(PyObject *self, PyObject *args)
{
    QueryAttributesObject *obj = (QueryAttributesObject *)self;
    obj->data->Notify();
    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
QueryAttributes_SetResultsMessage(PyObject *self, PyObject *args)
{
    QueryAttributesObject *obj = (QueryAttributesObject *)self;

    PyObject *packaged_args = 0;

    // Handle args packaged as first member of a tuple of size one
    // if we think the unpackaged args matches our needs
    if (PySequence_Check(args) && PySequence_Size(args) == 1)
    {
        packaged_args = PySequence_GetItem(args, 0);
        if (PyUnicode_Check(packaged_args))
            args = packaged_args;
    }

    if (!PyUnicode_Check(args))
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_TypeError, "arg is not a unicode string");
    }

    char const *val = PyUnicode_AsUTF8(args);
    std::string cval = std::string(val);

    if (val == 0 && PyErr_Occurred())
    {
        Py_XDECREF(packaged_args);
        PyErr_Clear();
        return PyErr_Format(PyExc_TypeError, "arg not interpretable as utf8 string");
    }

    Py_XDECREF(packaged_args);

    // Set the resultsMessage in the object.
    obj->data->SetResultsMessage(cval);

    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
QueryAttributes_GetResultsMessage(PyObject *self, PyObject *args)
{
    QueryAttributesObject *obj = (QueryAttributesObject *)self;
    PyObject *retval = PyString_FromString(obj->data->GetResultsMessage().c_str());
    return retval;
}

/*static*/ PyObject *
QueryAttributes_SetResultsValue(PyObject *self, PyObject *args)
{
    QueryAttributesObject *obj = (QueryAttributesObject *)self;

    doubleVector vec;

    if (PyNumber_Check(args))
    {
        double val = PyFloat_AsDouble(args);
        double cval = double(val);
        if (val == -1 && PyErr_Occurred())
        {
            PyErr_Clear();
            return PyErr_Format(PyExc_TypeError, "number not interpretable as C++ double");
        }
        if (fabs(double(val))>1.5E-7 && fabs((double(double(cval))-double(val))/double(val))>1.5E-7)
            return PyErr_Format(PyExc_ValueError, "number not interpretable as C++ double");
        vec.resize(1);
        vec[0] = cval;
    }
    else if (PySequence_Check(args) && !PyUnicode_Check(args))
    {
        vec.resize(PySequence_Size(args));
        for (Py_ssize_t i = 0; i < PySequence_Size(args); i++)
        {
            PyObject *item = PySequence_GetItem(args, i);

            if (!PyNumber_Check(item))
            {
                Py_DECREF(item);
                return PyErr_Format(PyExc_TypeError, "arg %d is not a number type", (int) i);
            }

            double val = PyFloat_AsDouble(item);
            double cval = double(val);

            if (val == -1 && PyErr_Occurred())
            {
                Py_DECREF(item);
                PyErr_Clear();
                return PyErr_Format(PyExc_TypeError, "arg %d not interpretable as C++ double", (int) i);
            }
            if (fabs(double(val))>1.5E-7 && fabs((double(double(cval))-double(val))/double(val))>1.5E-7)
            {
                Py_DECREF(item);
                return PyErr_Format(PyExc_ValueError, "arg %d not interpretable as C++ double", (int) i);
            }
            Py_DECREF(item);

            vec[i] = cval;
        }
    }
    else
        return PyErr_Format(PyExc_TypeError, "arg(s) must be one or more doubles");

    obj->data->GetResultsValue() = vec;
    // Mark the resultsValue in the object as modified.
    obj->data->SelectResultsValue();

    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
QueryAttributes_GetResultsValue(PyObject *self, PyObject *args)
{
    QueryAttributesObject *obj = (QueryAttributesObject *)self;
    // Allocate a tuple the with enough entries to hold the resultsValue.
    const doubleVector &resultsValue = obj->data->GetResultsValue();
    PyObject *retval = PyTuple_New(resultsValue.size());
    for(size_t i = 0; i < resultsValue.size(); ++i)
        PyTuple_SET_ITEM(retval, i, PyFloat_FromDouble(resultsValue[i]));
    return retval;
}

/*static*/ PyObject *
QueryAttributes_SetFloatResultsValue(PyObject *self, PyObject *args)
{
    QueryAttributesObject *obj = (QueryAttributesObject *)self;

    floatVector vec;

    if (PyNumber_Check(args))
    {
        double val = PyFloat_AsDouble(args);
        float cval = float(val);
        if (val == -1 && PyErr_Occurred())
        {
            PyErr_Clear();
            return PyErr_Format(PyExc_TypeError, "number not interpretable as C++ float");
        }
        if (fabs(double(val))>1.5E-7 && fabs((double(double(cval))-double(val))/double(val))>1.5E-7)
            return PyErr_Format(PyExc_ValueError, "number not interpretable as C++ float");
        vec.resize(1);
        vec[0] = cval;
    }
    else if (PySequence_Check(args) && !PyUnicode_Check(args))
    {
        vec.resize(PySequence_Size(args));
        for (Py_ssize_t i = 0; i < PySequence_Size(args); i++)
        {
            PyObject *item = PySequence_GetItem(args, i);

            if (!PyNumber_Check(item))
            {
                Py_DECREF(item);
                return PyErr_Format(PyExc_TypeError, "arg %d is not a number type", (int) i);
            }

            double val = PyFloat_AsDouble(item);
            float cval = float(val);

            if (val == -1 && PyErr_Occurred())
            {
                Py_DECREF(item);
                PyErr_Clear();
                return PyErr_Format(PyExc_TypeError, "arg %d not interpretable as C++ float", (int) i);
            }
            if (fabs(double(val))>1.5E-7 && fabs((double(double(cval))-double(val))/double(val))>1.5E-7)
            {
                Py_DECREF(item);
                return PyErr_Format(PyExc_ValueError, "arg %d not interpretable as C++ float", (int) i);
            }
            Py_DECREF(item);

            vec[i] = cval;
        }
    }
    else
        return PyErr_Format(PyExc_TypeError, "arg(s) must be one or more floats");

    obj->data->GetFloatResultsValue() = vec;
    // Mark the floatResultsValue in the object as modified.
    obj->data->SelectFloatResultsValue();

    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
QueryAttributes_GetFloatResultsValue(PyObject *self, PyObject *args)
{
    QueryAttributesObject *obj = (QueryAttributesObject *)self;
    // Allocate a tuple the with enough entries to hold the floatResultsValue.
    const floatVector &floatResultsValue = obj->data->GetFloatResultsValue();
    PyObject *retval = PyTuple_New(floatResultsValue.size());
    for(size_t i = 0; i < floatResultsValue.size(); ++i)
        PyTuple_SET_ITEM(retval, i, PyFloat_FromDouble(floatResultsValue[i]));
    return retval;
}

/*static*/ PyObject *
QueryAttributes_SetTimeStep(PyObject *self, PyObject *args)
{
    QueryAttributesObject *obj = (QueryAttributesObject *)self;

    PyObject *packaged_args = 0;

    // Handle args packaged into a tuple of size one
    // if we think the unpackaged args matches our needs
    if (PySequence_Check(args) && PySequence_Size(args) == 1)
    {
        packaged_args = PySequence_GetItem(args, 0);
        if (PyNumber_Check(packaged_args))
            args = packaged_args;
    }

    if (PySequence_Check(args))
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_TypeError, "expecting a single number arg");
    }

    if (!PyNumber_Check(args))
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_TypeError, "arg is not a number type");
    }

    long val = PyLong_AsLong(args);
    int cval = int(val);

    if (val == -1 && PyErr_Occurred())
    {
        Py_XDECREF(packaged_args);
        PyErr_Clear();
        return PyErr_Format(PyExc_TypeError, "arg not interpretable as C++ int");
    }
    if (fabs(double(val))>1.5E-7 && fabs((double(long(cval))-double(val))/double(val))>1.5E-7)
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_ValueError, "arg not interpretable as C++ int");
    }

    Py_XDECREF(packaged_args);

    // Set the timeStep in the object.
    obj->data->SetTimeStep(cval);

    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
QueryAttributes_GetTimeStep(PyObject *self, PyObject *args)
{
    QueryAttributesObject *obj = (QueryAttributesObject *)self;
    PyObject *retval = PyInt_FromLong(long(obj->data->GetTimeStep()));
    return retval;
}

/*static*/ PyObject *
QueryAttributes_SetXUnits(PyObject *self, PyObject *args)
{
    QueryAttributesObject *obj = (QueryAttributesObject *)self;

    PyObject *packaged_args = 0;

    // Handle args packaged as first member of a tuple of size one
    // if we think the unpackaged args matches our needs
    if (PySequence_Check(args) && PySequence_Size(args) == 1)
    {
        packaged_args = PySequence_GetItem(args, 0);
        if (PyUnicode_Check(packaged_args))
            args = packaged_args;
    }

    if (!PyUnicode_Check(args))
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_TypeError, "arg is not a unicode string");
    }

    char const *val = PyUnicode_AsUTF8(args);
    std::string cval = std::string(val);

    if (val == 0 && PyErr_Occurred())
    {
        Py_XDECREF(packaged_args);
        PyErr_Clear();
        return PyErr_Format(PyExc_TypeError, "arg not interpretable as utf8 string");
    }

    Py_XDECREF(packaged_args);

    // Set the xUnits in the object.
    obj->data->SetXUnits(cval);

    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
QueryAttributes_GetXUnits(PyObject *self, PyObject *args)
{
    QueryAttributesObject *obj = (QueryAttributesObject *)self;
    PyObject *retval = PyString_FromString(obj->data->GetXUnits().c_str());
    return retval;
}

/*static*/ PyObject *
QueryAttributes_SetYUnits(PyObject *self, PyObject *args)
{
    QueryAttributesObject *obj = (QueryAttributesObject *)self;

    PyObject *packaged_args = 0;

    // Handle args packaged as first member of a tuple of size one
    // if we think the unpackaged args matches our needs
    if (PySequence_Check(args) && PySequence_Size(args) == 1)
    {
        packaged_args = PySequence_GetItem(args, 0);
        if (PyUnicode_Check(packaged_args))
            args = packaged_args;
    }

    if (!PyUnicode_Check(args))
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_TypeError, "arg is not a unicode string");
    }

    char const *val = PyUnicode_AsUTF8(args);
    std::string cval = std::string(val);

    if (val == 0 && PyErr_Occurred())
    {
        Py_XDECREF(packaged_args);
        PyErr_Clear();
        return PyErr_Format(PyExc_TypeError, "arg not interpretable as utf8 string");
    }

    Py_XDECREF(packaged_args);

    // Set the yUnits in the object.
    obj->data->SetYUnits(cval);

    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
QueryAttributes_GetYUnits(PyObject *self, PyObject *args)
{
    QueryAttributesObject *obj = (QueryAttributesObject *)self;
    PyObject *retval = PyString_FromString(obj->data->GetYUnits().c_str());
    return retval;
}

/*static*/ PyObject *
QueryAttributes_SetFloatFormat(PyObject *self, PyObject *args)
{
    QueryAttributesObject *obj = (QueryAttributesObject *)self;

    PyObject *packaged_args = 0;

    // Handle args packaged as first member of a tuple of size one
    // if we think the unpackaged args matches our needs
    if (PySequence_Check(args) && PySequence_Size(args) == 1)
    {
        packaged_args = PySequence_GetItem(args, 0);
        if (PyUnicode_Check(packaged_args))
            args = packaged_args;
    }

    if (!PyUnicode_Check(args))
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_TypeError, "arg is not a unicode string");
    }

    char const *val = PyUnicode_AsUTF8(args);
    std::string cval = std::string(val);

    if (val == 0 && PyErr_Occurred())
    {
        Py_XDECREF(packaged_args);
        PyErr_Clear();
        return PyErr_Format(PyExc_TypeError, "arg not interpretable as utf8 string");
    }

    Py_XDECREF(packaged_args);

    // Set the floatFormat in the object.
    obj->data->SetFloatFormat(cval);

    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
QueryAttributes_GetFloatFormat(PyObject *self, PyObject *args)
{
    QueryAttributesObject *obj = (QueryAttributesObject *)self;
    PyObject *retval = PyString_FromString(obj->data->GetFloatFormat().c_str());
    return retval;
}

/*static*/ PyObject *
QueryAttributes_SetXmlResult(PyObject *self, PyObject *args)
{
    QueryAttributesObject *obj = (QueryAttributesObject *)self;

    PyObject *packaged_args = 0;

    // Handle args packaged as first member of a tuple of size one
    // if we think the unpackaged args matches our needs
    if (PySequence_Check(args) && PySequence_Size(args) == 1)
    {
        packaged_args = PySequence_GetItem(args, 0);
        if (PyUnicode_Check(packaged_args))
            args = packaged_args;
    }

    if (!PyUnicode_Check(args))
    {
        Py_XDECREF(packaged_args);
        return PyErr_Format(PyExc_TypeError, "arg is not a unicode string");
    }

    char const *val = PyUnicode_AsUTF8(args);
    std::string cval = std::string(val);

    if (val == 0 && PyErr_Occurred())
    {
        Py_XDECREF(packaged_args);
        PyErr_Clear();
        return PyErr_Format(PyExc_TypeError, "arg not interpretable as utf8 string");
    }

    Py_XDECREF(packaged_args);

    // Set the xmlResult in the object.
    obj->data->SetXmlResult(cval);

    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
QueryAttributes_GetXmlResult(PyObject *self, PyObject *args)
{
    QueryAttributesObject *obj = (QueryAttributesObject *)self;
    PyObject *retval = PyString_FromString(obj->data->GetXmlResult().c_str());
    return retval;
}

/*static*/ PyObject *
QueryAttributes_SetQueryInputParams(PyObject *self, PyObject *args)
{
    QueryAttributesObject *obj = (QueryAttributesObject *)self;

    (void) obj;
    // NOT IMPLEMENTED!!!
    // name=queryInputParams, type=MapNode

    Py_INCREF(Py_None);
    return Py_None;
}

/*static*/ PyObject *
QueryAttributes_GetQueryInputParams(PyObject *self, PyObject *args)
{
    QueryAttributesObject *obj = (QueryAttributesObject *)self;
    (void) obj;
    // NOT IMPLEMENTED!!!
    // name=queryInputParams, type=MapNode
    PyObject *retval = NULL;
    return retval;
}



PyMethodDef PyQueryAttributes_methods[QUERYATTRIBUTES_NMETH] = {
    {"Notify", QueryAttributes_Notify, METH_VARARGS},
    {"SetResultsMessage", QueryAttributes_SetResultsMessage, METH_VARARGS},
    {"GetResultsMessage", QueryAttributes_GetResultsMessage, METH_VARARGS},
    {"SetResultsValue", QueryAttributes_SetResultsValue, METH_VARARGS},
    {"GetResultsValue", QueryAttributes_GetResultsValue, METH_VARARGS},
    {"SetFloatResultsValue", QueryAttributes_SetFloatResultsValue, METH_VARARGS},
    {"GetFloatResultsValue", QueryAttributes_GetFloatResultsValue, METH_VARARGS},
    {"SetTimeStep", QueryAttributes_SetTimeStep, METH_VARARGS},
    {"GetTimeStep", QueryAttributes_GetTimeStep, METH_VARARGS},
    {"SetXUnits", QueryAttributes_SetXUnits, METH_VARARGS},
    {"GetXUnits", QueryAttributes_GetXUnits, METH_VARARGS},
    {"SetYUnits", QueryAttributes_SetYUnits, METH_VARARGS},
    {"GetYUnits", QueryAttributes_GetYUnits, METH_VARARGS},
    {"SetFloatFormat", QueryAttributes_SetFloatFormat, METH_VARARGS},
    {"GetFloatFormat", QueryAttributes_GetFloatFormat, METH_VARARGS},
    {"SetXmlResult", QueryAttributes_SetXmlResult, METH_VARARGS},
    {"GetXmlResult", QueryAttributes_GetXmlResult, METH_VARARGS},
    {"SetQueryInputParams", QueryAttributes_SetQueryInputParams, METH_VARARGS},
    {"GetQueryInputParams", QueryAttributes_GetQueryInputParams, METH_VARARGS},
    {NULL, NULL}
};

//
// Type functions
//

static void
QueryAttributes_dealloc(PyObject *v)
{
   QueryAttributesObject *obj = (QueryAttributesObject *)v;
   if(obj->parent != 0)
       Py_DECREF(obj->parent);
   if(obj->owns)
       delete obj->data;
}

static PyObject *QueryAttributes_richcompare(PyObject *self, PyObject *other, int op);
PyObject *
PyQueryAttributes_getattr(PyObject *self, char *name)
{
    if(strcmp(name, "resultsMessage") == 0)
        return QueryAttributes_GetResultsMessage(self, NULL);
    if(strcmp(name, "resultsValue") == 0)
        return QueryAttributes_GetResultsValue(self, NULL);
    if(strcmp(name, "floatResultsValue") == 0)
        return QueryAttributes_GetFloatResultsValue(self, NULL);
    if(strcmp(name, "timeStep") == 0)
        return QueryAttributes_GetTimeStep(self, NULL);
    if(strcmp(name, "xUnits") == 0)
        return QueryAttributes_GetXUnits(self, NULL);
    if(strcmp(name, "yUnits") == 0)
        return QueryAttributes_GetYUnits(self, NULL);
    if(strcmp(name, "floatFormat") == 0)
        return QueryAttributes_GetFloatFormat(self, NULL);
    if(strcmp(name, "xmlResult") == 0)
        return QueryAttributes_GetXmlResult(self, NULL);
    if(strcmp(name, "queryInputParams") == 0)
        return QueryAttributes_GetQueryInputParams(self, NULL);


    // Add a __dict__ answer so that dir() works
    if (!strcmp(name, "__dict__"))
    {
        PyObject *result = PyDict_New();
        for (int i = 0; PyQueryAttributes_methods[i].ml_meth; i++)
            PyDict_SetItem(result,
                PyString_FromString(PyQueryAttributes_methods[i].ml_name),
                PyString_FromString(PyQueryAttributes_methods[i].ml_name));
        return result;
    }

    return Py_FindMethod(PyQueryAttributes_methods, self, name);
}

int
PyQueryAttributes_setattr(PyObject *self, char *name, PyObject *args)
{
    PyObject NULL_PY_OBJ;
    PyObject *obj = &NULL_PY_OBJ;

    if(strcmp(name, "resultsMessage") == 0)
        obj = QueryAttributes_SetResultsMessage(self, args);
    else if(strcmp(name, "resultsValue") == 0)
        obj = QueryAttributes_SetResultsValue(self, args);
    else if(strcmp(name, "floatResultsValue") == 0)
        obj = QueryAttributes_SetFloatResultsValue(self, args);
    else if(strcmp(name, "timeStep") == 0)
        obj = QueryAttributes_SetTimeStep(self, args);
    else if(strcmp(name, "xUnits") == 0)
        obj = QueryAttributes_SetXUnits(self, args);
    else if(strcmp(name, "yUnits") == 0)
        obj = QueryAttributes_SetYUnits(self, args);
    else if(strcmp(name, "floatFormat") == 0)
        obj = QueryAttributes_SetFloatFormat(self, args);
    else if(strcmp(name, "xmlResult") == 0)
        obj = QueryAttributes_SetXmlResult(self, args);

    if (obj != NULL && obj != &NULL_PY_OBJ)
        Py_DECREF(obj);

    if (obj == &NULL_PY_OBJ)
    {
        obj = NULL;
        PyErr_Format(PyExc_NameError, "name '%s' is not defined", name);
    }
    else if (obj == NULL && !PyErr_Occurred())
        PyErr_Format(PyExc_RuntimeError, "unknown problem with '%s'", name);

    return (obj != NULL) ? 0 : -1;
}

static int
QueryAttributes_print(PyObject *v, FILE *fp, int flags)
{
    QueryAttributesObject *obj = (QueryAttributesObject *)v;
    fprintf(fp, "%s", PyQueryAttributes_ToString(obj->data, "",false).c_str());
    return 0;
}

PyObject *
QueryAttributes_str(PyObject *v)
{
    QueryAttributesObject *obj = (QueryAttributesObject *)v;
    return PyString_FromString(PyQueryAttributes_ToString(obj->data,"", false).c_str());
}

//
// The doc string for the class.
//
#if PY_MAJOR_VERSION > 2 || (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION >= 5)
static const char *QueryAttributes_Purpose = "This class contains attributes used for query.";
#else
static char *QueryAttributes_Purpose = "This class contains attributes used for query.";
#endif

//
// Python Type Struct Def Macro from Py2and3Support.h
//
//         VISIT_PY_TYPE_OBJ( VPY_TYPE,
//                            VPY_NAME,
//                            VPY_OBJECT,
//                            VPY_DEALLOC,
//                            VPY_PRINT,
//                            VPY_GETATTR,
//                            VPY_SETATTR,
//                            VPY_STR,
//                            VPY_PURPOSE,
//                            VPY_RICHCOMP,
//                            VPY_AS_NUMBER)

//
// The type description structure
//

VISIT_PY_TYPE_OBJ(QueryAttributesType,         \
                  "QueryAttributes",           \
                  QueryAttributesObject,       \
                  QueryAttributes_dealloc,     \
                  QueryAttributes_print,       \
                  PyQueryAttributes_getattr,   \
                  PyQueryAttributes_setattr,   \
                  QueryAttributes_str,         \
                  QueryAttributes_Purpose,     \
                  QueryAttributes_richcompare, \
                  0); /* as_number*/

//
// Helper function for comparing.
//
static PyObject *
QueryAttributes_richcompare(PyObject *self, PyObject *other, int op)
{
    // only compare against the same type 
    if ( Py_TYPE(self) != &QueryAttributesType
         || Py_TYPE(other) != &QueryAttributesType)
    {
        Py_INCREF(Py_NotImplemented);
        return Py_NotImplemented;
    }

    PyObject *res = NULL;
    QueryAttributes *a = ((QueryAttributesObject *)self)->data;
    QueryAttributes *b = ((QueryAttributesObject *)other)->data;

    switch (op)
    {
       case Py_EQ:
           res = (*a == *b) ? Py_True : Py_False;
           break;
       case Py_NE:
           res = (*a != *b) ? Py_True : Py_False;
           break;
       default:
           res = Py_NotImplemented;
           break;
    }

    Py_INCREF(res);
    return res;
}

//
// Helper functions for object allocation.
//

static QueryAttributes *defaultAtts = 0;
static QueryAttributes *currentAtts = 0;

static PyObject *
NewQueryAttributes(int useCurrent)
{
    QueryAttributesObject *newObject;
    newObject = PyObject_NEW(QueryAttributesObject, &QueryAttributesType);
    if(newObject == NULL)
        return NULL;
    if(useCurrent && currentAtts != 0)
        newObject->data = new QueryAttributes(*currentAtts);
    else if(defaultAtts != 0)
        newObject->data = new QueryAttributes(*defaultAtts);
    else
        newObject->data = new QueryAttributes;
    newObject->owns = true;
    newObject->parent = 0;
    return (PyObject *)newObject;
}

static PyObject *
WrapQueryAttributes(const QueryAttributes *attr)
{
    QueryAttributesObject *newObject;
    newObject = PyObject_NEW(QueryAttributesObject, &QueryAttributesType);
    if(newObject == NULL)
        return NULL;
    newObject->data = (QueryAttributes *)attr;
    newObject->owns = false;
    newObject->parent = 0;
    return (PyObject *)newObject;
}

///////////////////////////////////////////////////////////////////////////////
//
// Interface that is exposed to the VisIt module.
//
///////////////////////////////////////////////////////////////////////////////

PyObject *
QueryAttributes_new(PyObject *self, PyObject *args)
{
    int useCurrent = 0;
    if (!PyArg_ParseTuple(args, "i", &useCurrent))
    {
        if (!PyArg_ParseTuple(args, ""))
            return NULL;
        else
            PyErr_Clear();
    }

    return (PyObject *)NewQueryAttributes(useCurrent);
}

//
// Plugin method table. These methods are added to the visitmodule's methods.
//
static PyMethodDef QueryAttributesMethods[] = {
    {"QueryAttributes", QueryAttributes_new, METH_VARARGS},
    {NULL,      NULL}        /* Sentinel */
};

static Observer *QueryAttributesObserver = 0;

std::string
PyQueryAttributes_GetLogString()
{
    std::string s("QueryAtts = QueryAttributes()\n");
    if(currentAtts != 0)
        s += PyQueryAttributes_ToString(currentAtts, "QueryAtts.", true);
    return s;
}

static void
PyQueryAttributes_CallLogRoutine(Subject *subj, void *data)
{
    typedef void (*logCallback)(const std::string &);
    logCallback cb = (logCallback)data;

    if(cb != 0)
    {
        std::string s("QueryAtts = QueryAttributes()\n");
        s += PyQueryAttributes_ToString(currentAtts, "QueryAtts.", true);
        cb(s);
    }
}

void
PyQueryAttributes_StartUp(QueryAttributes *subj, void *data)
{
    if(subj == 0)
        return;

    currentAtts = subj;
    PyQueryAttributes_SetDefaults(subj);

    //
    // Create the observer that will be notified when the attributes change.
    //
    if(QueryAttributesObserver == 0)
    {
        QueryAttributesObserver = new ObserverToCallback(subj,
            PyQueryAttributes_CallLogRoutine, (void *)data);
    }

}

void
PyQueryAttributes_CloseDown()
{
    delete defaultAtts;
    defaultAtts = 0;
    delete QueryAttributesObserver;
    QueryAttributesObserver = 0;
}

PyMethodDef *
PyQueryAttributes_GetMethodTable(int *nMethods)
{
    *nMethods = 1;
    return QueryAttributesMethods;
}

bool
PyQueryAttributes_Check(PyObject *obj)
{
    return (obj->ob_type == &QueryAttributesType);
}

QueryAttributes *
PyQueryAttributes_FromPyObject(PyObject *obj)
{
    QueryAttributesObject *obj2 = (QueryAttributesObject *)obj;
    return obj2->data;
}

PyObject *
PyQueryAttributes_New()
{
    return NewQueryAttributes(0);
}

PyObject *
PyQueryAttributes_Wrap(const QueryAttributes *attr)
{
    return WrapQueryAttributes(attr);
}

void
PyQueryAttributes_SetParent(PyObject *obj, PyObject *parent)
{
    QueryAttributesObject *obj2 = (QueryAttributesObject *)obj;
    obj2->parent = parent;
}

void
PyQueryAttributes_SetDefaults(const QueryAttributes *atts)
{
    if(defaultAtts)
        delete defaultAtts;

    defaultAtts = new QueryAttributes(*atts);
}

